package com.lanou.myapp.comic.recommendedcomic;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.myapp.R;
import com.lanou.myapp.comic.recommendedcomic.contentrecommendedcomic.ContentRecommendedcomicActivity;
import com.lanou.myapp.imageloader.GsonRequest;
import com.lanou.myapp.imageloader.VolleySingleton;
import com.lanou.myapp.progressdialog.Dialog;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by dllo on 16/1/11.
 */
public class RecommendedComicFragment extends Fragment {
    private String url = "http://api.alithefox.net/comic_lists/1?limit=20&offset=0";
    private Context context;
    private RecyclerView recyclerView;
    private RequestQueue queue;
    private RecyclerRecommendedAdapter adapter;
    private List<ComicsBeans.DataEntity.ComicsEntity> data;
    private Dialog dialog;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView_recommended_comic);
        adapter = new RecyclerRecommendedAdapter(getActivity());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        dialog = new Dialog(context,"jiazai");
        dialog.setTitle("正在加载，请稍后...");
        dialog.show();

        GsonRequest<ComicsBeans> request = new GsonRequest<ComicsBeans>(url, ComicsBeans.class, new Response.Listener<ComicsBeans>() {
            @Override
            public void onResponse(final ComicsBeans response) {
                adapter = new RecyclerRecommendedAdapter(getActivity());
                adapter.addData(response.getData().getComics());
//                Log.d("com",response.getData().getComics().size() + "");
                recyclerView.setAdapter(adapter);

                dialog.dismiss();
                adapter.setMyOnclick(new MyOnclick() {
                    @Override
                    public void MyonClick(int position) {
                        Intent intent = new Intent(getActivity(), ContentRecommendedcomicActivity.class);
                        intent.putExtra("Id", response.getData().getComics().get(position).getComic_id());
                        getActivity().startActivity(intent);
                    }
                });


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(request);


//        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
//            @Override
//            public void onResponse(String response) {
//                data = new ArrayList<>();
//                Gson gson = new Gson();
//                ComicsBeans comicsBeans = gson.fromJson(response,ComicsBeans.class);
//                data =comicsBeans.getData().getComics();
//                adapter.addData(data);
//
//
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        });
//        queue.add(request);
//
//
//        adapter.setMyOnclick(new MyOnclick() {
//            @Override
//            public void MyonClick(int position) {
//                Intent intent = new Intent(getActivity(),ContentRecommendedcomicActivity.class);
//                intent.putExtra("Id",data.get(position).getComic_id());
//                getActivity().startActivity(intent);
//            }
//        });

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_recommended_comic, null);
        return view;
    }

}
